package org.ewaf.framework.struts2.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;


public class DateFormatUtil {
	
	private final static String INPUT_DATE = "yyyy-MM-dd";
	private final static String INPUT_DATETIME = "yyyy-MM-dd HH:mm";
	
	private final static String DISPLAY_DATE = "yyyy-MM-dd";
	private final static String DISPLAY_DATETIME = "yyyy-MM-dd HH:mm";
	
	private final static String DISPLAY_DATESECOND = "yyyy-MM-dd HH:mm:ss";
	public static Date parseDate(String text) {
		if (StringUtils.isBlank(text)) {
			return null;
		}
		
		try {
			return new SimpleDateFormat(INPUT_DATE).parse(text);
		} catch (ParseException e) {
			throw new IllegalArgumentException(e);
		}
	}
	
	public static Date parseDateTime(String text) {
		if (StringUtils.isBlank(text)) {
			return null;
		}
		
		try {
			return new SimpleDateFormat(INPUT_DATETIME).parse(text);
		} catch (ParseException e) {
			throw new IllegalArgumentException(e);
		}
	}
	
	public static String date(Object date) {
		if (date == null) {
			return "";
		}
		
		if (date instanceof Date) {
			return new SimpleDateFormat(DISPLAY_DATE).format(date);
		} else {
			return date.toString();
		}
	}
	
	public static String dateTime(Object datetime) {
		if (datetime == null) {
			return "";
		}
		
		if (datetime instanceof Date) {
			return new SimpleDateFormat(DISPLAY_DATETIME).format(datetime);
		} else {
			return datetime.toString();
		}
	}
	public static String dateSecond(Object datetime) {
		if (datetime == null) {
			return "";
		}
		
		if (datetime instanceof Date) {
			return new SimpleDateFormat(DISPLAY_DATESECOND).format(datetime);
		} else {
			return datetime.toString();
		}
	}
	public static Date now() {
		return new Date();
	}
	
}
